home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / unarced / utilities / emulators / apple][ / curses.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  6KB  |  297 lines

  1. /*
  2.  *  a2, an Apple II emulator in C
  3.  *  (c) Copyright 1990 by Rich Skrenta
  4.  *
  5.  *  Command line interface written by Tom Markson
  6.  *
  7.  *  Distribution agreement:
  8.  *
  9.  *    You may freely copy or redistribute this software, so long
  10.  *    as there is no profit made from its use, sale, trade or
  11.  *    reproduction.  You may not change this copyright notice,
  12.  *    and it must be included prominently in any copy made.
  13.  *
  14.  *  Send emulator related mail to:  skrenta@blekko.commodore.com
  15.  *                    skrenta@blekko.uucp
  16.  */
  17.  
  18. /*
  19. **  This a screen management library borrowed with permission from the
  20. **  Elm mail system (a great mailer--I highly recommend it!).
  21. **
  22. **  I've hacked this library to only provide what I need.
  23. **
  24. **  Original copyright follows:
  25. */
  26. /*******************************************************************************
  27.  *  The Elm Mail System  -  $Revision: 2.1 $   $State: Exp $
  28.  *
  29.  *             Copyright (c) 1986 Dave Taylor
  30.  ******************************************************************************/
  31.  
  32. #ifndef M_XENIX
  33. #include <sys/termio.h>
  34. #endif
  35.  
  36. #include <stdio.h>
  37. #include <curses.h>
  38. #include <ctype.h>
  39.  
  40. #define DEFAULT_term_lines    24
  41. #define DEFAULT_COLUMNS    80
  42. #define TTYIN    0
  43.  
  44.  
  45. #define        VERY_LONG_STRING    2500
  46.  
  47. int term_lines = DEFAULT_term_lines - 1;
  48. int term_cols  = DEFAULT_COLUMNS;
  49. extern char escape_char;
  50.  
  51. #ifdef SHORTNAMES
  52. # define _cleartoeoln    _clrtoeoln
  53. # define _cleartoeos    _clr2eos
  54. #endif
  55.  
  56. #ifndef BSD
  57. struct termio raw_tty, 
  58.               orig_tty;
  59. #else
  60. #define TCGETA    TIOCGETP
  61. #define TCSETAW    TIOCSETP
  62.  
  63. struct sgttyb raw_tty,
  64.           orig_tty;
  65. #endif
  66.  
  67. static int inraw = 0;                  /* are we IN rawmode?    */
  68.  
  69.  
  70. static int _memory_locked = 0;        /* are we IN memlock??   */
  71.  
  72. static int _intransmit;            /* are we transmitting keys? */
  73.  
  74. static
  75. char *_clearscreen, *_moveto, *_cleartoeoln, *_cleartoeos,
  76.     *_setinverse, *_clearinverse;
  77.  
  78.  
  79. static
  80. int _lines,_columns;
  81.  
  82. static char _terminal[1024];              /* Storage for terminal entry */
  83. static char _capabilities[1024];           /* String for cursor motion */
  84.  
  85. static char *ptr = _capabilities;    /* for buffering         */
  86.  
  87. int    outchar();            /* char output for tputs */
  88. char  *tgetstr(),                    /* Get termcap capability */
  89.       *tgoto();                /* and the goto stuff    */
  90.  
  91. InitScreen()
  92. {
  93. int  tgetent(),      /* get termcap entry */
  94.      err;
  95. char termname[40];
  96. char *strcpy(), *getenv();
  97.     
  98.     if (getenv("TERM") == NULL) {
  99.         fprintf(stderr,
  100.           "TERM variable not set; Screen capabilities required\n");
  101.         return(FALSE);
  102.     }
  103.     if (strcpy(termname, getenv("TERM")) == NULL) {
  104.         fprintf(stderr,"Can't get TERM variable\n");
  105.         return(FALSE);
  106.     }
  107.     if ((err = tgetent(_terminal, termname)) != 1) {
  108.         fprintf(stderr,"Can't get entry for TERM\n");
  109.         return(FALSE);
  110.     }
  111.  
  112.     /* load in all those pesky values */
  113.     _clearscreen       = tgetstr("cl", &ptr);
  114.     _moveto            = tgetstr("cm", &ptr);
  115.     _cleartoeoln       = tgetstr("ce", &ptr);
  116.     _cleartoeos        = tgetstr("cd", &ptr);
  117.     _setinverse        = tgetstr("so", &ptr);
  118.     _clearinverse      = tgetstr("se", &ptr);
  119.     _lines                 = tgetnum("li");
  120.     _columns       = tgetnum("co");
  121.  
  122.     if (!_clearscreen) {
  123.         fprintf(stderr,
  124.             "Terminal must have clearscreen (cl) capability\n");
  125.         return(FALSE);
  126.     }
  127.     if (!_moveto) {
  128.         fprintf(stderr,
  129.             "Terminal must have cursor motion (cm)\n");
  130.         return(FALSE);
  131.     }
  132.     if (!_cleartoeoln) {
  133.         fprintf(stderr,
  134.             "Terminal must have clear to end-of-line (ce)\n");
  135.         return(FALSE);
  136.     }
  137.     if (!_cleartoeos) {
  138.         fprintf(stderr,
  139.             "Terminal must have clear to end-of-screen (cd)\n");
  140.         return(FALSE);
  141.     }
  142.     if (_lines == -1)
  143.         _lines = DEFAULT_term_lines;
  144.     if (_columns == -1)
  145.         _columns = DEFAULT_COLUMNS;
  146.     return(TRUE);
  147. }
  148.  
  149. ScreenSize(lines, columns)
  150. int *lines, *columns;
  151. {
  152.     /** returns the number of lines and columns on the display. **/
  153.  
  154.     if (_lines == 0) _lines = DEFAULT_term_lines;
  155.     if (_columns == 0) _columns = DEFAULT_COLUMNS;
  156.  
  157.     *lines = _lines - 1;        /* assume index from zero*/
  158.     *columns = _columns;        /* assume index from one */
  159. }
  160.  
  161. ClearScreen()
  162. {
  163.     /* clear the screen: returns -1 if not capable */
  164.  
  165.     tputs(_clearscreen, 1, outchar);
  166.     fflush(stdout);      /* clear the output buffer */
  167. }
  168.  
  169. MoveCursor(row, col)
  170. int row, col;
  171. {
  172.     /** move cursor to the specified row column on the screen.
  173.             0,0 is the top left! **/
  174.  
  175.     char *stuff, *tgoto();
  176.  
  177.     stuff = tgoto(_moveto, col, row);
  178.     tputs(stuff, 1, outchar);
  179. /*    fflush(stdout);    */
  180. }
  181.  
  182. CleartoEOLN()
  183. {
  184.     /** clear to end of line **/
  185.  
  186.     tputs(_cleartoeoln, 1, outchar);
  187.     fflush(stdout);  /* clear the output buffer */
  188. }
  189.  
  190. CleartoEOS()
  191. {
  192.     /** clear to end of screen **/
  193.  
  194.     tputs(_cleartoeos, 1, outchar);
  195.     fflush(stdout);  /* clear the output buffer */
  196. }
  197.  
  198. Raw(state)
  199. int state;
  200. {
  201.     /** state is either TRUE or FALSE, as indicated by call **/
  202.  
  203.     if (state == FALSE && inraw) {
  204.       (void) ioctl(TTYIN, TCSETAW, &orig_tty);
  205.       inraw = 0;
  206.     }
  207.     else if (state == TRUE && ! inraw) {
  208.  
  209.       (void) ioctl(TTYIN, TCGETA, &orig_tty);    /** current setting **/
  210.  
  211.       (void) ioctl(TTYIN, TCGETA, &raw_tty);    /** again! **/
  212. #ifdef BSD
  213. /*      raw_tty.sg_flags &= ~(ECHO | CRMOD);    /* echo off */
  214.       raw_tty.sg_flags &= ~ECHO;        /* echo off */
  215.       raw_tty.sg_flags |= CBREAK;    /* raw on    */
  216. #else
  217.       raw_tty.c_lflag &= ~(ICANON | ECHO);    /* noecho raw mode        */
  218.  
  219.       raw_tty.c_cc[VMIN] = '\01';    /* minimum # of chars to queue    */
  220.       raw_tty.c_cc[VTIME] = '\0';    /* minimum time to wait for input */
  221.  
  222. #endif
  223.       raw_tty.c_cc[VINTR]= escape_char;
  224.       (void) ioctl(TTYIN, TCSETAW, &raw_tty);
  225.       inraw = 1;
  226.     }
  227. }
  228.  
  229. int
  230. ReadCh()
  231. {
  232.     /** read a character with Raw mode set! **/
  233.  
  234.     register int result;
  235.     char ch;
  236.     result = read(0, &ch, 1);
  237.         return((result <= 0 ) ? EOF : ch);
  238. }
  239.  
  240.  
  241. outchar(c)
  242. char c;
  243. {
  244.     /** output the given character.  From tputs... **/
  245.     /** Note: this CANNOT be a macro!              **/
  246.  
  247.     putc(c, stdout);
  248. }
  249.  
  250.  
  251. #if 0
  252. static int inverse = FALSE;
  253.  
  254. SetInverse() {
  255.  
  256.     if (!inverse) {
  257.         StartInverse();
  258.         inverse = TRUE;
  259.     }
  260. }
  261.  
  262. SetNormal() {
  263.  
  264.     if (inverse) {
  265.         EndInverse();
  266.         inverse = FALSE;
  267.     }
  268. }
  269. #endif
  270.  
  271.  
  272. StartInverse()
  273. {
  274.     /** set inverse video mode **/
  275.  
  276.     if (!_setinverse)
  277.         return(-1);
  278.  
  279.     tputs(_setinverse, 1, outchar);
  280. /*    fflush(stdout);    */
  281.     return(0);
  282. }
  283.  
  284.  
  285. EndInverse()
  286. {
  287.     /** compliment of startinverse **/
  288.  
  289.     if (!_clearinverse)
  290.         return(-1);
  291.  
  292.     tputs(_clearinverse, 1, outchar);
  293. /*    fflush(stdout);    */
  294.     return(0);
  295. }
  296.  
  297.